package com.marui.ch1.vola;

/**
 * 演示violate无法提供操作的原子性
 * @author marui
 * @date 2020/4/9 9:28
 * @since 1.0
 */
public class VolatileUnsafe {

	int a = 0;

	public void addA(){
		a++;
	}
	
    public static void main(String[] args) throws InterruptedException {
		VolatileUnsafe volatileUnsafe = new VolatileUnsafe();
		for (int i = 0; i < 20000; i++) {
			new Thread(() -> {
				for (int j = 0; j < 20; j++) {
					volatileUnsafe.addA();
				}
			}).start();
		}

		Thread.sleep(3000);

		System.out.println("a is "+volatileUnsafe.a);
	}

}
